﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Net;
using TopoMap.Coordinates;

namespace TopoMap.GoogleMaps
{
    public class GgMap
    {
        private static string uri = "http://maps.google.com/staticmap?center={0},{1}&zoom=16&size={2}x{2}&key=ABQIAAAAYxC8BTKC4XyJZVpsj3OFqhTBR6Sw60Yy9kIk0wQsvMNO-8wughRUDkNkNDLYoFwmdCAC89zY41U8pA&sensor=true&markers={0},{1},blues&format=JPEG&maptype=hybrid";
        //private static string uri = "http://maps.google.com/staticmap?center={0},{1}&zoom=16&size={2}x{2}&key=MAPS_API_KEY&sensor=true_or_false&markers={0},{1},blues";

        private PolarCoordinate g84Coord;
        private Bitmap bmp;

        public PolarCoordinate G84Coord
        {
            get { return g84Coord; }
        }
        public Bitmap Bmp
        {
            get { return bmp; }
        }

        public GgMap(PolarCoordinate g84Coord, int size)
        {
            this.g84Coord = g84Coord;
            try
            {
                System.Uri uriIgn = new Uri(string.Format(uri, g84Coord.Longitud.ToString("F6"), g84Coord.Latitud.ToString("F6"), size));
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriIgn.AbsoluteUri);
                bmp = new Bitmap(request.GetResponse().GetResponseStream());
            }
            catch (Exception e)
            {

                bmp = new Bitmap(size, size);
                using (Graphics errorGraphics = Graphics.FromImage(bmp))
                {
                    errorGraphics.FillRectangle(new SolidBrush(Color.Coral), 0, 0, bmp.Width, bmp.Height);
                    Brush myBrush = new SolidBrush(Color.Red);
                    Font myFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular);
                    RectangleF layoutRectangle = new RectangleF(1, 1, size, size);
                    errorGraphics.DrawString(e.Message, myFont, myBrush, layoutRectangle);
                }
            }
        }
    }
}
